<template>
  <div class="who-exhibits">
    <div
      class="page-header"
      :style="bannerImage ? { backgroundImage: `url(${bannerImage})` } : {}"
      :class="{ 'has-bg': !!bannerImage }"
    >
      <div class="container">
        <h1 class="page-title">谁来观展</h1>
        <p class="page-subtitle">了解观众来源及分布</p>
      </div>
    </div>
    <!-- 导航菜单 -->
    <div class="nav-menu">
      <div class="container">
        <nav class="about-nav">
          <a
            @click="setActiveTab('why-exhibit')"
            :class="['nav-item', { active: activeTab === 'why-exhibit' }]"
          >
            为何参展
          </a>
          <a
            @click="setActiveTab('who-exhibits')"
            :class="['nav-item', { active: activeTab === 'who-exhibits' }]"
          >
            谁来观展
          </a>
          <a
            @click="setActiveTab('exhibit-registration')"
            :class="[
              'nav-item',
              { active: activeTab === 'exhibit-registration' },
            ]"
          >
            参展报名
          </a>
          <!-- <a
            @click="setActiveTab('exhibitor-system')"
            :class="['nav-item', { active: activeTab === 'exhibitor-system' }]"
          >
            展商系统
          </a> -->
          <a
            @click="setActiveTab('exhibitor-services')"
            :class="[
              'nav-item',
              { active: activeTab === 'exhibitor-services' },
            ]"
          >
            展商服务
          </a>
          <a
            @click="setActiveTab('exhibitor-reviews')"
            :class="['nav-item', { active: activeTab === 'exhibitor-reviews' }]"
          >
            展商评语
          </a>
        </nav>
      </div>
    </div>

    <div class="container">
      <section class="content-section">
        <!-- <h2 class="section-title">谁来观展</h2>
        <p v-if="pageData.description" class="section-desc">
          {{ pageData.description }}
        </p> -->

        <!-- 多条数据展示：图片在上，文本在下 -->
        <div class="exhibitors-grid">
          <div
            class="exhibitor-item"
            v-for="exhibitor in exhibitorTypes"
            :key="exhibitor.id"
          >
            <!-- 图片在上 -->
            <div class="exhibitor-image-section" v-if="exhibitor.image">
              <img
                :src="exhibitor.image"
                :alt="exhibitor.title"
                class="exhibitor-image"
              />
            </div>
            <!-- 文本在下 -->
            <div class="exhibitor-content">
              <h3 class="exhibitor-title">{{ exhibitor.title }}</h3>
              <div
                v-html="exhibitor.description"
                class="exhibitor-description"
              ></div>
            </div>
          </div>
        </div>
      </section>
    </div>
  </div>
</template>

<script>
import { ref, onMounted } from "vue";
import {
  OfficeBuilding,
  Shop,
  House,
  Setting,
  Tools,
  Grid,
  DataAnalysis,
  Picture,
  Promotion,
} from "@element-plus/icons-vue";
import { subpageApi } from "@/api/user";

export default {
  name: "WhoExhibits",
  setup() {
    const activeTab = ref("who-exhibits");
    // 页面基本信息
    const pageData = ref({});
    const bannerImage = ref("");

    // 参展企业数据
    const exhibitorTypes = ref([]);

    // 加载谁来观展数据
    const loadWhoExhibitsData = async () => {
      try {
        const response = await subpageApi.getSubpageList(
          "exhibitors",
          "who-exhibit"
        );
        if (
          response &&
          response.code === "200" &&
          response.data &&
          response.data.length > 0
        ) {
          // 取第一条作为页面基本信息
          const firstItem = response.data[0];
          pageData.value = {
            title: firstItem.title || "谁来观展",
            description:
              firstItem.description ||
              (firstItem.content &&
              !(firstItem.content || "").trim().startsWith("[") &&
              !(firstItem.content || "").trim().startsWith("{")
                ? firstItem.content
                : ""),
          };

          // 处理多条参展企业数据
          exhibitorTypes.value = response.data.map((item, index) => {
            const parsed = parseContent(item.content);
            return {
              id: item.id || index + 1,
              title: item.title || "",
              description: parsed.content || "",
              image: item.imageUrl || parsed.backgroundImage || "",
            };
          });
        } else {
          // 使用默认数据
          pageData.value = getDefaultPageData();
          exhibitorTypes.value = getDefaultExhibitorTypes();
        }
      } catch (error) {
        console.error("加载谁来观展数据失败:", error);
        pageData.value = getDefaultPageData();
        exhibitorTypes.value = getDefaultExhibitorTypes();
      }
    };

    // 解析富文本内容
    const parseContent = (content) => {
      if (!content)
        return {
          content: "",
          backgroundImage: "",
          companyCount: 0,
          boothCount: 0,
          percentage: 0,
        };

      try {
        if (typeof content === "string" && content.trim().startsWith("{")) {
          return JSON.parse(content);
        }
        return {
          content: content,
          backgroundImage: "",
          companyCount: 0,
          boothCount: 0,
          percentage: 0,
        };
      } catch (e) {
        return {
          content: content,
          backgroundImage: "",
          companyCount: 0,
          boothCount: 0,
          percentage: 0,
        };
      }
    };

    // 获取默认图标
    const getDefaultIcon = (index) => {
      const icons = [
        "OfficeBuilding",
        "Shop",
        "House",
        "Setting",
        "Tools",
        "Grid",
        "DataAnalysis",
        "Picture",
        "Promotion",
      ];
      return icons[index % icons.length];
    };

    // 获取图标组件
    const getIconComponent = (iconName) => {
      const iconMap = {
        OfficeBuilding: OfficeBuilding,
        Shop: Shop,
        House: House,
        Setting: Setting,
        Tools: Tools,
        Grid: Grid,
        DataAnalysis: DataAnalysis,
        Picture: Picture,
        Promotion: Promotion,
      };
      return iconMap[iconName] || OfficeBuilding;
    };

    // 默认参展企业类型数据
    const getDefaultExhibitorTypes = () => {
      return [
        {
          id: 1,
          title: "智能制造企业",
          description:
            "<p>专注于智能制造技术、工业自动化设备、机器人技术等领域的创新企业，展示最新的智能制造解决方案。</p><p>这些企业代表了制造业的未来发展方向，通过技术创新推动产业升级。</p>",
          image:
            "https://images.unsplash.com/photo-1581091226825-a6a2a5aee158?w=400&h=300&fit=crop",
        },
        {
          id: 2,
          title: "工业软件公司",
          description:
            "<p>提供工业软件解决方案、数据分析平台、智能制造管理系统等软件服务的企业。</p><p>通过数字化技术帮助企业提升生产效率和管理水平。</p>",
          image:
            "https://images.unsplash.com/photo-1551288049-bebda4e38f71?w=400&h=300&fit=crop",
        },
        {
          id: 3,
          title: "设备制造商",
          description:
            "<p>专业制造工业设备、生产设备、检测设备等硬件设备的企业，展示先进的制造工艺。</p><p>为制造业提供高质量的生产设备和解决方案。</p>",
          image:
            "https://images.unsplash.com/photo-1565043589221-1a6fd9a6d0b8?w=400&h=300&fit=crop",
        },
        {
          id: 4,
          title: "技术服务商",
          description:
            "<p>提供技术咨询、系统集成、维护服务等技术服务的企业，为制造业提供专业支持。</p><p>帮助企业实现技术升级和数字化转型。</p>",
          image:
            "https://images.unsplash.com/photo-1507003211169-0a1dd7228f2d?w=400&h=300&fit=crop",
        },
      ];
    };

    // 默认页面数据
    const getDefaultPageData = () => {
      return {
        title: "谁来观展",
        description: "了解参展企业类型和行业分布，发现更多商业合作机会",
      };
    };

    const setActiveTab = (tabKey) => {
      if (!tabKey) return;
      activeTab.value = tabKey;
      // 跳转到对应的子页面
      if (tabKey !== "who-exhibits") {
        window.location.href = `/${tabKey}`;
      }
    };

    const loadBanner = async () => {
      try {
        const resp = await subpageApi.getContentByType("carousel_exhibitors");
        if (
          resp &&
          resp.code === "200" &&
          Array.isArray(resp.data) &&
          resp.data.length
        ) {
          const first =
            resp.data.find((it) => it.status === "active") || resp.data[0];
          const content =
            typeof first.content === "string"
              ? JSON.parse(first.content || "{}")
              : first.content || {};
          bannerImage.value = first.imageUrl || content.image || "";
        }
      } catch (e) {
        bannerImage.value = "";
      }
    };

    onMounted(() => {
      loadWhoExhibitsData();
      loadBanner();
    });

    return {
      activeTab,
      pageData,
      bannerImage,
      exhibitorTypes,
      setActiveTab,
    };
  },
};
</script>

<style scoped>
.who-exhibits {
  min-height: 100vh;
  background: #f8f9fa;
}

:deep(.resizable-image) {
  max-width: 100% !important;
  height: auto !important;
}

.page-header {
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  padding: 120px 0 80px 0;
  margin-top: 0;
  text-align: center;
  color: white;
}

.page-header.has-bg {
  height: 400px;
  position: relative;
  background: none;
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

.page-header.has-bg::before {
  content: "";
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
}

.page-header.has-bg .container {
  position: relative;
  z-index: 1;
}

.page-title {
  font-size: 3rem;
  font-weight: 700;
  margin-bottom: 1rem;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
}

.page-subtitle {
  font-size: 1.2rem;
  opacity: 0.9;
  margin-bottom: 0;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.content-section {
  background: white;
  border-radius: 20px;
  padding: 3rem;
  margin: 2rem 0;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
}

.section-title {
  font-size: 2rem;
  color: #333;
  margin-bottom: 2rem;
  text-align: center;
  position: relative;
  padding-left: 15px;
}

.section-title::before {
  content: "";
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 4px;
  height: 24px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 2px;
}

.section-desc {
  margin: -8px 0 2rem;
  color: #666;
  text-align: center;
  font-size: 1.1rem;
  line-height: 1.6;
}

/* 多条数据网格布局 */
.exhibitors-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 2rem;
  /* margin-top: 2rem; */
}

.exhibitor-item {
  background: white;
  border-radius: 15px;
  overflow: hidden;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.exhibitor-item:hover {
  transform: translateY(-5px);
  box-shadow: 0 15px 30px rgba(0, 0, 0, 0.15);
}

/* 图片在上 */
.exhibitor-image-section {
  width: 100%;
  height: 200px;
  overflow: hidden;
}

.exhibitor-image {
  width: 100%;
  height: 100%;
  object-fit: contain;
  background-color: #f8f9fa;
}

/* 文本在下 */
.exhibitor-content {
  padding: 1.5rem;
}

.exhibitor-title {
  font-size: 1.3rem;
  font-weight: 600;
  color: #333;
  margin-bottom: 1rem;
  text-align: center;
}

.exhibitor-description {
  color: #666;
  line-height: 1.6;
  text-align: left;
}

/* 富文本内图片限制 */
.exhibitor-description :deep(img) {
  max-width: 100%;
  height: auto;
  display: block;
  margin: 10px 0;
  border-radius: 8px;
}

@media (max-width: 768px) {
  .page-title {
    font-size: 2rem;
  }

  .content-section {
    padding: 2rem;
    margin: 1rem 0;
  }

  .section-title {
    font-size: 1.5rem;
  }

  .exhibitors-grid {
    grid-template-columns: 1fr;
    gap: 1.5rem;
  }
}

/* 导航菜单样式 - 参考关于我们页面 */
.nav-menu {
  background: #f8f9fa;
  padding: 20px 0;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.about-nav {
  display: flex;
  justify-content: center;
  gap: 30px;
  flex-wrap: wrap;
}

.nav-item {
  text-decoration: none;
  color: #333;
  font-size: 1.1rem;
  font-weight: 500;
  padding: 10px 20px;
  border-radius: 8px;
  transition: background-color 0.3s ease, color 0.3s ease;
  border: 1px solid #eee;
  cursor: pointer;
}

.nav-item:hover {
  background-color: var(--e-global-color-selected);
  color: white;
  border-color: var(--e-global-color-selected);
}

.nav-item.active {
  background-color: var(--e-global-color-selected);
  color: white;
  border-color: var(--e-global-color-selected);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .about-nav {
    gap: 15px;
  }

  .nav-item {
    font-size: 1rem;
    padding: 8px 16px;
  }
}
</style>
